package com.can.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.can.common.validate.InsertValidate;
import com.can.common.validate.UpdateValidate;
import com.can.entity.CPage;
import com.can.entity.Resource;
import com.can.entityDTO.JsonResult;
import com.can.entityDTO.ResourceDTO;
import com.can.service.resource.IResourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Desription 用户资源控制器
 * @Author can
 * @Date 2018/10/22
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/resource")
public class ResourceController extends BaseController<Resource>{
    @Autowired
    private IResourceService resourceService;

    /**
     * 保存
     * @param resourceDTO
     * @return
     */
    @PostMapping("/save")
    public JsonResult save(@Validated(value = InsertValidate.class) @RequestBody ResourceDTO resourceDTO) throws InstantiationException, IllegalAccessException {
        Resource resource = getEntity(resourceDTO,Resource.class);
        Resource fResource = resourceService.selectById(resourceDTO.getfId());
        if(StringUtils.isEmpty(fResource.getfIds())){
            resource.setfIds(resource.getfId().toString());
        }else{
            resource.setfIds(fResource.getfIds() + "/" + resource.getfId().toString());
        }
        Integer result = resourceService.save(getEntity(resourceDTO,Resource.class));
        return JsonResult.createSuccess(result);
    }

    /**
     * 更新
     * @param resourceDTO
     * @return
     */
    @PutMapping("/update")
    public JsonResult update(@Validated(value = UpdateValidate.class) @RequestBody ResourceDTO resourceDTO) throws InstantiationException, IllegalAccessException {
        Boolean result = resourceService.updateById(getEntity(resourceDTO,Resource.class));
        return JsonResult.createSuccess(result);
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("/select/{id}")
    public JsonResult selectById(@PathVariable("id") Integer id){
        Resource result = resourceService.selectById(id);
        return JsonResult.createSuccess(result);
    }

    @PostMapping("/selectPage")
    public JsonResult selectPage(@Validated @RequestBody CPage<Resource> page){
        Page<Resource> pageParam = new Page<Resource>();
        BeanUtils.copyProperties(page,pageParam);
        EntityWrapper<Resource> eWrapper = new EntityWrapper<Resource>(page.getEntity());
        Page<Resource> result = resourceService.selectPage(pageParam,eWrapper);
        return JsonResult.createSuccess(result);
    }

    @PostMapping("/selectList")
    public JsonResult selectList(@RequestBody ResourceDTO resourceDTO) throws InstantiationException, IllegalAccessException {
        List<Resource> result = resourceService.selectList(getEntity(resourceDTO,Resource.class));
        return JsonResult.createSuccess(result);
    }

    @DeleteMapping("/delete/{id}")
    public JsonResult deleteById(@PathVariable("id") Integer id){
        Boolean result = resourceService.deleteById(id);
        return JsonResult.createSuccess(result);
    }

    @DeleteMapping("/deleteBatchIds")
    public JsonResult deleteBatchIds(@RequestBody List<Integer> idList){
        Boolean result = resourceService.deleteBatchIds(idList);
        return JsonResult.createSuccess(result);
    }
}
